<?php

/**
 * main file that routes to other files
 */
$debug = 2;
error_reporting(E_ALL);
include './conf/config.php';
include './helper/helper.php';

//parse post and get variable
sanitizeInput();

define('ROOTDIR', dirname(__FILE__));
define('DEF', 'bukhari');
define('APPPATH', ROOTDIR . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR);

//database connection
$db = new PDO("mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbName'] . "", $dbConfig['username'], $dbConfig['password'], array(PDO::ATTR_PERSISTENT => true));

$segments = getUrlSegments();

$fileName = '';
if (!$segments[0]) {
    $fileName = APPPATH . DEF . DIRECTORY_SEPARATOR . 'index.php';
} else {
    $fileName .= APPPATH;
    $fileName .= $segments[0] . DIRECTORY_SEPARATOR;
    if (isset($segments[1])) {
        $fileName .= $segments[1] . '.php';
    } else {
        $fileName .= 'index.php';
    }
}

if (!file_exists($fileName)) {
    include APPPATH . '404.php';
} else {
    include $fileName;
}